home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Demos / A.D. Software / OOFILE / Buildable, limited OOFILE / samples / ooftst03.cpp < prev    next >
C/C++ Source or Header  |  1995-12-26  |  3KB  |  86 lines

  1. // Copyright 1994 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST3
  4.  
  5. // this sample tries a number of random updates to test m/u
  6.  
  7. // Simple stream I/O is used to interact with the user.
  8. #include "oofile.hpp"
  9.  
  10. #include "ooftst02.inc"
  11.  
  12. #ifdef _Windows
  13. #include <windows.h>
  14. void yieldTime();
  15. void yieldTime() {
  16.     MSG msg;
  17.     PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE);
  18. }
  19. #endif
  20.  
  21. int main()
  22. {
  23.     cout << "OOFILE Validation Suite - Test 3\n"
  24.          << "Simple random updates testing multi-user" << endl
  25.          << "using a copy of the database from ooftest2" << endl;
  26.  
  27. // get probably remote file path
  28.     char remotePath[255];
  29.     cout << "Enter full file path, probably a remote disk: " << endl 
  30.          << "(eg: Mac Remote Disk:oofile:ooftst03.db  OR  E:\\OOFILE\\OOFTEST3.DB)" << endl;
  31.     cin.getline(remotePath, 255);
  32.     
  33.     cout << endl << "Are we running as a Writer or Reader (R/W)? ";
  34.     char writeORread;
  35.     cin >> writeORread;
  36.     bool asWriter = ( (writeORread=='W') || (writeORread=='w') );
  37.  
  38. //    if (dbConnect::fileExists("ooftest3.db"))
  39. // JUST ASSUME IT DOES - this function doesn't work if file already open!
  40.         theDB.openConnection(remotePath);
  41. //    else {
  42. //        dbConnect::raise("Please copy ooftest2.db (from running ooftest2) to ooftest3.db");
  43. //    }
  44.     
  45.     if (asWriter) {
  46.         cout << "Repeatedly updating the first record..." << endl;
  47.         unsigned long numRecs = Patients.count();
  48.         for (unsigned long i=0; i<100000 ; i++) {
  49.             unsigned long theRec = rand()%numRecs;
  50.             
  51.     // if turning on locking, uncomment here and at end of this block
  52.     // but remember 1 writer/many readers doesn't require you to do anything
  53.             theDB.enterWriteLocking();
  54.             Patients.start(); // comment out this line if you want random updates
  55. // IF YOU WANT RANDOM                    Patients[theRec];    // jump to a random record
  56.             cout << "Writing rec: " << theRec << '\t' << Patients.LastName << " " << Patients.Salary;
  57.             Patients.Salary = Patients.Salary + 1;
  58.             Patients.saveRecord();    
  59.             Patients.unloadRecord();  // force read from disk
  60.             cout << " ... " << Patients.Salary << endl;
  61.             theDB.exitLocking();
  62. #ifdef _Windows
  63.             yieldTime();
  64. #endif
  65.         }
  66.     }
  67.     else {
  68.         cout << "Randomly reading records..." << endl;
  69.         unsigned long numRecs = Patients.count();
  70.         for (unsigned long i=0; i<100000 ; i++) {
  71.             unsigned long theRec = rand()%numRecs;
  72.             
  73.             Patients[theRec];    // jump to a random record
  74.             cout << "rec: " << theRec << '\t' << Patients.LastName << " " << Patients.Salary << endl;
  75. #ifdef _Windows
  76.             yieldTime();
  77. #endif
  78.         }
  79.     }
  80.  
  81.     cout << "Database after the random updates" << endl << Patients << endl;
  82.     cout << "done" << endl;
  83.  
  84.     return EXIT_SUCCESS;
  85. }
  86.